$(function () {
    $("#email").val("");
    $("#code").val("");
    getUrl("retrievePassword/selectorMode");
    //实时监控输入框
    inputChange("#email","#emailError");
    inputChange("#code","#codeError");
    function emailMessage(){
        var email = $("#email").val();
        if(email==null||email==""){
            validateSpanMsg("emailError","请输入邮箱");
            return false;
        }else if(!isEmail(email)){
            validateSpanMsg("emailError","邮箱格式不正确");
            return false;
        }else{
            return true;
        }
    }
    function codeMessage(){
        var code = $("#code").val();
        if(code==null||code==""){
            validateSpanMsg("codeError","请输入验证码");
            return false;
        }else if(!/^[A-Za-z0-9]{6}$/.test(code)){
            validateSpanMsg("codeError","验证码错误");
            return false;
        }else{
            return true;
        }
    }
    $("#email").blur(function(){
        emailMessage();
    })
    $("#code").blur(function(){
        codeMessage();
    })
    $("#get").click(function () {
        var email = $("#email").val();
        if(emailMessage()){
            $("#get").attr("disabled", "disabled");
            $(this).val("发送中...");
            ajaxGetCodeToEmail(email,this);
        }
    });
    $("#sub").click(function () {
        var code = $("#code").val();
        var email = $("#email").val();
        if(emailMessage()&&codeMessage()){
            $("#sub").attr("disabled", "disabled");
            ajaxValidateCodeOfEmail(code);
        }

    });
});

/**
 * 发送邮箱验证码
 * @param email
 * @param that
 */
function ajaxGetCodeToEmail(email,that) {
    if (isEmail(email)) {
        var url = "/Lvscore_User_M/ajaxGetCodeToEmail";
        $.post(
            url,
            {'email': email},
            function (data) {
                if (data == 1) {
                    alertBox("发送成功,请注意查收");
                    //倒计时
                    settime(that);
                } else {
                    //alertBox("您输入的邮箱未注册！","");
                    setTimeout(function(){
                        validateSpanMsg("emailError","您输入的邮箱未注册");
                        that.removeAttribute("disabled");
                        that.value = "获取邮箱验证码";
                        that.style.opacity = "1";
                    },1000);
                }
            })
    }
}

/**
 * 邮箱方式重置密码验证码校验
 * @param code    用户输入验证码
 */
function ajaxValidateCodeOfEmail(code) {
    if (/^[A-Za-z0-9]{6}$/.test(code)) {
        var url = "/Lvscore_User_M/ajaxValidateCodeOfEmail";
        $.post(
            url,
            {'code': code},
            function (data) {
                $("#sub").removeAttr("disabled", "disabled");
                if (data == 1) {
                    window.location.href = "/Lvscore_User_M/userWeb/views/retrievePassword/resetMail.html";
                } else {
                    validateSpanMsg("codeError","验证码输入错误");
                }
            });
    } else {
        $("#sub").removeAttr("disabled", "disabled");
        validateSpanMsg("codeError","验证码输入错误");
    }
}